/**
 * CS543 HW4 Joseph Meyer
 */
package edu.src.address;

/**
 * Given an address, extracts the page number and offset
 * 
 * 
 * 32 bit Integer:
 * 
 *    address             page#    offset
 * 31----------------16-15-----8-7-------0
 * 
 *            bits 
 *            
 * @author meyer
 */
public abstract class Address {
	
	private Integer address;
	private int pageNumber;
	private int offset;
	private ReadWriteBit dirtyBit;
	
	/**
	 * Constructor.
	 * Given a 32 bit address, will decipher a page by and offset
	 * @param address - 32 bit Integer
	 * @param bit - String. The Read/Write indicator for the dirty bit
	 */
	public Address(Integer address, ReadWriteBit bit) {
		
		try {
			this.address = address;
			int pageTemp = address & AddressReaderUtils.MASK_PAGEBY;
			this.pageNumber = pageTemp >>> 8; //8 bits to the right
			this.offset = address & AddressReaderUtils.MASK_OFFSET;
			this.dirtyBit = bit;
			
		} catch (Exception e) {
			System.err.println(e);
			
		}
	}

	/**
	 * Constructor.
	 * Given a 32 bit address, will decipher a page by and offset
	 * @param address - 32 bit Integer
	 */
	public Address(Integer address) {
		this(address, ReadWriteBit.NONE);
	}
	
	/**
	 * @return ReadWriteBit
	 */
	public ReadWriteBit getDirtyBit() {
		return dirtyBit;
	}
	
	/**
	 * @param dirtyBit the dirtyBit to set
	 */
	public void setDirtyBit(ReadWriteBit dirtyBit) {
		this.dirtyBit = dirtyBit;
	}

	/**
	 * @return Integer
	 */
	public Integer getAddress() {
		return address;
	}

	/**
	 * @return int
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * @return int
	 */
	public int getOffset() {
		return offset;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Address [address=" + address + ", pageNumber=" + pageNumber
				+ ", offset=" + offset + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + offset;
		result = prime * result + pageNumber;
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Address other = (Address) obj;
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (offset != other.offset)
			return false;
		if (pageNumber != other.pageNumber)
			return false;
		return true;
	}
	
	

}
